package com.dhcc.core.framework.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

/**
 * 数组工具类
 * 
 * @ClassName: ArrayUtil
 * @Description: TODO
 * @author: cyf
 * @date: 2018年1月2日 上午9:30:55
 */
public class ArrayUtil extends cn.hutool.core.util.ArrayUtil {
    
    public static final String DOT = ".";
    public static final String UNDERLINE = "_";
    public static final String COMMA = ",";
    public static final String SEMICOLON = ";";

    /**
     * 数组转化为字符串
     * 
     * @param objs
     *            数组
     * @param separator
     *            分隔符
     * @return 返回数组转化成功后的字符串;失败返回：null
     */
    public static String arrayToString(Object[] objs, String separator) {
        StringBuffer strBuff = null;
        if (null != objs && objs.length > 0) {
            if (CommonUtil.isEmpty(separator)) {
                separator = "";
            }
            strBuff = new StringBuffer();
            for (int i = 0; i < objs.length; i++) {
                if (i < objs.length - 1) {
                    strBuff.append(String.valueOf(objs[i]) + separator);
                } else {
                    strBuff.append(String.valueOf(objs[i]));
                }

            }
        }
        objs = null;
        return (null != strBuff) ? strBuff.toString() : null;
    }

    /**
     * 字符串转化为数组
     * 
     * @param value
     *            原字符串
     * @param separator
     *            分隔符
     * @return 返回字符串分割成功后的数组
     */
    public static String[] stringToArray(String value, String separator) {
        String[] array = null;
        if (CommonUtil.isNotEmpty(value) && CommonUtil.isNotEmpty(separator)) {
            array = StringUtils.split(value,separator);
        }
        value = null;
        return array;
    }

    /**
     * 字符串转化为数组
     * 
     * @param value
     *            原字符串
     * @param separator
     *            分隔符
     * @return 返回字符串分割成功后的数组
     */
    public static Integer[] stringToIntArray(String value, String separator) {
        String[] array = null;
        Integer[] intArray = null;
        if (!CommonUtil.isEmpty(value) && !CommonUtil.isEmpty(separator)) {
            array = value.split(separator);
            intArray = new Integer[array.length];
            for (int i = 0; i < array.length; i++) {
                if(CommonUtil.isNotEmpty(array[i])){
                    intArray[i] = Integer.parseInt(array[i]);
                }
            }
        }
        value = null;
        return intArray;
    }

    /**
     * 字符串转化为数组
     * 
     * @param value
     *            原字符串
     * @param separator
     *            分隔符
     * @return 返回字符串分割成功后的数组
     */
    public static Long[] stringToLongArray(String value, String separator) {
        String[] array = null;
        Long[] longArray = null;
        if (!CommonUtil.isEmpty(value) && !CommonUtil.isEmpty(separator)) {
            array = value.split(separator);
            longArray = new Long[array.length];
            for (int i = 0; i < array.length; i++) {
                if(CommonUtil.isNotEmpty(array[i])){
                    longArray[i] = Long.parseLong(array[i]);
                }
            }
        }
        value = null;
        return longArray;
    }
    /**
     * 字符串转化为数组去掉“[]”
     *
     * @param value
     *            原字符串
     * @param separator
     *            分隔符
     * @return 返回字符串分割成功后的数组
     */
    public static Long[] stringformatToArray(String value, String separator) {
        String[] array = null;
        Long[] longArray = null;
        if (!CommonUtil.isEmpty(value) && !CommonUtil.isEmpty(separator)) {
            array = value.split(separator);
            longArray = new Long[array.length];
            for (int i = 0; i < array.length; i++) {
                if(CommonUtil.isNotEmpty(array[i])){
                    array[i] = array[i].replaceAll("[^0-9]","");
                    longArray[i] = Long.parseLong(array[i]);
                }
            }
        }
        value = null;
        return longArray;
    }

    /**
     * 按separater分离成数组,判断该数组里面是否包含subStr
     * 
     * @param str
     * @param subStr
     * @param separater
     * @return 包含返回：true；否则返回：false
     */
    public static boolean isArrayContains(String str, String subStr, String separater) {
        boolean is = false;
        if (null != str) {
            if (null == subStr) {
                is = true;
            } else {
                String[] strArray = str.split(separater);
                for (int i = 0; i < strArray.length; i++) {
                    if (subStr.equals(strArray[i].trim())) {
                        is = true;
                        break;
                    }
                }
            }
        }
        return is;
    }

    /**
     * 判断该数组里面是否包含subObj
     * 
     * @param objs
     * @param subObj
     * @return 包含返回：true；否则返回：false
     */
    public static boolean isArrayContains(Object[] objs, Object subObj) {
        boolean is = false;
        if (null == subObj) {
            is = true;
        }
        if (objs.length > 0 && null != subObj) {
            for (Object obj : objs) {
                if (subObj.toString().equals(obj.toString())) {
                    is = true;
                    break;
                }
            }
        } else
            is = false;
        return is;
    }

    /**
     * 按separater分离成数组,判断该数组里面是否包含subStr(不区分大小写)
     * 
     * @param str
     * @param subStr
     * @param separater
     * @return 包含返回：true；否则返回：false
     */
    public static boolean isArrayContainsIgnoreCase(String str, String subStr, String separater) {
        boolean is = false;
        if (null != str) {
            if (null == subStr) {
                is = true;
            } else {
                String[] strArray = str.split(separater);
                for (int i = 0; i < strArray.length; i++) {
                    if (subStr.equalsIgnoreCase(strArray[i].trim())) {
                        is = true;
                        break;
                    }
                }
            }
        }
        return is;
    }

    /**
     * 数组转化为List
     * @param <T>
     * 
     * @param objs
     *            数组
     * @return 返回数组转化成功后的List;失败返回：null
     */
    public static <T> List<T> arrayToList(T[] objs) {
        List<T> ls = new ArrayList<T>();
        Collections.addAll(ls, objs);
        return ls;
    }

}
